<?php
namespace OpenApi\Controller;
use Think\Controller;
class LoginController extends Controller{
	const key = '250olederp';//key

	public function _initialize()
	{
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		//数据库连接基础配置-end
	}

	/*
	 * 登录
	 * @param $username 账户
	 * @param $password 密码
	 * @param $type 账户类型 1:主账号 0:从账号
	 * @param $company 公司名称
	 * @param $sign 签名
	 * */
	public function loginCheck($username,$password,$type,$company,$sign){
		$signData['username'] = $username;
		$signData['password'] = $password;
		$signData['type'] = $type;
		$signData['company'] = $company;
		$token = $this->api_token($signData,self::key,date('Y-m-d'));

		if ($sign != $token){
			alert_json_new(0,'签名验证失败');
		}

		if($type!=0 && $type!=1){
			alert_json_new(0,'操作有误');
		}
		if($username==''){
			alert_json_new(0,'用户名不能为空');
		}
		// if(!mobile($username) && $type==1){
		// alert(0,'手机号码错误');
		// }
		if($password==''){
			alert_json_new(0,'密码不能为空');
		}
		if($type==0 && $company==''){
			alert_json_new(0,'企业名称不能为空');
		}
		$where['username']=$username;
		if($type==1){//主账号
			$result=M('user')->where($where)->find();
			if(empty($result)){
				$result=M('user')->where(array('nick_name'=>$username))->find();
				if(empty($result)){
					alert_json_new(0,"没有此账号");
				}
			}
			if($result['status']==0){
				alert_json_new(0,'正在审核！');
			}
			if($result['status']==2){
				alert_json_new(0,'未通过审核！');
			}
			if(count($result)==0){
				alert_json_new(0,'用户名不存在');
			}
			if($result['password']==md5($password. $result['salt'])){
				$result['db']=C('DB_PREFIX_T').$result['username'];
				$result['type']=1;
				$result['company_id']=$result['id'];
				session('company',$result);
				// $book=A('Book');
				// $book->syn_book(1);//账册同步
				alert_json_new(1,'登录成功');
			}else{
				alert_json_new(0,'密码错误');
			}
		}else{///次账号
			$user=M('user')->where(array('company'=>$company))->find();
			$user['db']=C('DB_PREFIX_T').$user['username'];
			$m_username=$user['username'];
			if($m_username===null){
				alert_json_new(0,'公司名称不存在');
			}
			$db_name=C('DB_PREFIX_T').$m_username;

			C('erp_dbname',$db_name);
			C('db_config.DB_NAME',C('erp_dbname'));
			$m = M('role',C('DB_PREFIX_T'), C('db_config'));

			$result=$m->where($where)->find();
			if(empty($result)){
				$result=$m->where(array('nick_name'=>$username))->find();
				if(empty($result)){
					alert_json_new(0,"没有此账号");
				}
			}
			if(count($result)==0){
				alert_json_new(0,'用户名不存在');
			}
			if($result['password']==md5($password. $result['salt'])){
				$result['db']=C('DB_PREFIX_T').$m_username;
				// $result['type']=0;
				$result['company_id']=$user['id'];
				$user['type']=0;
				$result['company']=$user['company'];
				session('company',$user);//主账号
				session('company.childs',$result);//子账号
				$res=$m->where($where)->setInc("login_count");
				if(!$res){
					$m->where(array('nick_name'=>$username))->setInc("login_count");
				}
				$res=$m->where($where)->setField("last_login_date",date("Y-m-d H:i:s",time()));
				if(!$res){
					$res=$m->where(array('nick_name'=>$username))->setField("last_login_date",date("Y-m-d H:i:s",time()));
				}
				// $book=A('Book');
				// $book->syn_book(1);//账册同步
				alert_json_new(1,'登录成功');
			}else{
				alert_json_new(0,'密码错误');
			}
		}
	}

	/*
	 * token
	 * @param $data数据
	 * @param $key key
	 * @param $date 日期
	 * */
	public function api_token($data,$key,$date)
	{
		$api_token = md5(http_build_query($data).$key.$date);
		return $api_token;
	}
}